{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<!-- （勿改动，执行即可）执行更改背景 -->\n",
       "<link rel=\"stylesheet\" href=\"exam.css\" type=\"text/css\">\n",
       "<h1 style=\"color: red;\">注意单元格的第一行不能改动，否则会影响自动打分</h1>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<!-- （勿改动，执行即可）执行更改背景 -->\n",
    "<link rel=\"stylesheet\" href=\"exam.css\" type=\"text/css\">\n",
    "<h1 style=\"color: red;\">注意单元格的第一行不能改动，否则会影响自动打分</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "# Python 期中考（C卷）\n",
       "* 共6题，第四题40分，其余每题20分，最高分140，作题时间90分钟。\n",
       "*  答题格首行如 ***# 003*** 勿删除或改动 \n",
       "* 可先挑难度较易的题先做，🌶个数越高越难\n",
       "* 执行一格格，最后一格可回报分数（仅供参考）\n",
       " ##提交此.ipynb档，必检查： \n",
       "   * 档名✍C_学号✍（只能用半角数字9码）\n",
       "   *  下格 输入学号（半角数字9码） \n",
       "\n",
       "\n",
       "# 🛂输入学号🛂"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 始000（勿改动，执行即可）\n",
    "e = %env\n",
    "_which_= \"C\"  # 卷号\n",
    "import PandasCourse as PC\n",
    "from IPython.display import Markdown\n",
    "Markdown(PC.msgs['opening'].format(w=_which_))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 始001（✍请改动並执行）\n",
    "student_id = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读入数据\n",
    "* 以下代码是读入相关文本数据\n",
    "* 所有文本数据赋值给text\n",
    "* 以下考题皆为针对此text做相关操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 直接执行，执行完请观察text文本\n",
    "import pandas as pd\n",
    "df = pd.read_csv('Combined_374.csv','\\t')\n",
    "df_summary = df[\"AB\"].fillna(\"NAN\").tolist()\n",
    "text = \"\".join(df_summary) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 英文数据：空格隔开的单词组成\n",
    "# 中文数据：没有空格\n",
    "# text "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q1（20分） 🌶 易\n",
    "* 查找\"媒体\"的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1527"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请勿改动变量名freq_table_phrase，过程可自行书写\n",
    "# 知识点： \n",
    "# 1. 使用变量\n",
    "# 2. count（使用在序列数据当中，如字符串，列表，元组，可快速查询词频）\n",
    "#  两个思路： 1. python/doc   2. 百度/必应浏览器搜索关键词\n",
    "phrase = \"媒体\"\n",
    "freq_table_phrase= text.count(phrase)       # str.count()\n",
    "freq_table_phrase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q2（20分） 🌶 易\n",
    "* 用中文\"。\"拆分,生成list_split列表，每一个句子是一个独立的列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"python\".split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "# text.split(\"。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请勿改动变量名list_split，过程可自行书写\n",
    "list_split = text.split(\"。\")\n",
    "# list_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q3 （20分） 🌶 易\n",
    "* 取出第十个句子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1033"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一共有多少句？\n",
    "len(list_split)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'在媒体融合中,优质内容资源的合理运营是传统出版企业实现转型升级的基础'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请勿改动变量名the_10_phrase，过程可自行书写\n",
    "# 知识点：\n",
    "# 1. 列表的取值[index], （切片-没考，可以回顾）\n",
    "# 2. 清楚列表的序列是从0开始的\n",
    "the_10_phrase= list_split[9]\n",
    "the_10_phrase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q4 （40分） 🌶🌶  中\n",
    "* 请找出text中所有\"媒体\"关键字前面的两个字符\n",
    "\n",
    "> 1. 先找出所有媒体位置**列表** position_all，（20分）\n",
    "> 2. 再找出所有 \"媒体\"关键字前面的两个字符**列表**  content_all，（20分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "答案提示:\n",
    "[95, 144, 154, 164, 204, 235, 356, 400, ...]\n",
    "['提出', '应新', '重新', '用新', '建新', ...]\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'媒体'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phrase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "list_text = ['提出','应新','重新','媒体','媒体']\n",
    "for i,j in enumerate(list_text):\n",
    "    if j == '媒体':\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Q4 ： test 代码块\n",
    "text # 是一个字符串\n",
    "# 知识点： \n",
    "# 1. enumerate 作为枚举，对序列数据进行  索引值 和 值（一起循环打印结果）\n",
    "# 2. list列表的新增 append\n",
    "position_all=[]\n",
    "for i,j in enumerate(text):\n",
    "    if j == '媒':\n",
    "        if text[i+1] == '体':\n",
    "            position_all.append(i)\n",
    "# print(position_all)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Q4 test2 测试代码块\n",
    "# 知识点：\n",
    "# 1. 序列数据的切片\n",
    "# 2. 列表的新增 append\n",
    "content_all=[]\n",
    "for i in position_all:\n",
    "#     print(text[i]+text[i+1]) 字符串拼接\n",
    "#     print(text[i-2:i]) # 切片\n",
    "    content_all.append(text[i-2:i])\n",
    "# print(content_all)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q5 （20分） 🌶🌶  中\n",
    "* 统计text中所有\"媒体\"关键字前面的两个字符的出现次数（即词频）\n",
    "\n",
    "```\n",
    "答案示例：\n",
    "{'提出': 2, '应新': 3, '重新': 1, '用新': 3, '建新': 1,...}\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 知识点\n",
    "# 字典的内容：\n",
    "# 1. 字典的新建（3种方法其中一种）  dict_name[key] = value (常用)\n",
    "# 2. 列表的词频 （序列数据的词频） count\n",
    "found = {}\n",
    "for i in content_all:\n",
    "    found[i]=content_all.count(i)\n",
    "# print(found)    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q6 （20分） 🌶 🌶 🌶 稍难\n",
    "* 找出text中所有\"媒体\"关键字前面的两个字符的次数排在前八的关键词，作为一个新的字典输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 1, 3, 1, 1, 169, 1, 21, 15, 1, 6, 2, 1, 5, 1, 8, 2, 5, 38, 1, 2, 1, 3, 9, 5, 16, 15, 1, 2, 1, 8, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 3, 1, 1, 2, 3, 2, 1, 1, 4, 5, 3, 1, 6, 5, 1, 1, 1, 1, 1, 6, 1, 2, 4, 1, 3, 2, 6, 6, 15, 6, 3, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 38, 1, 1, 13, 5, 2, 1, 1, 1, 13, 1, 2, 2, 19, 2, 2, 2, 1, 3, 4, 4, 1, 4, 1, 1, 21, 2, 1, 10, 1, 1, 1, 1, 5, 3, 1, 1, 3, 1, 2, 1, 1, 5, 1, 1, 2, 2, 4, 1, 2, 7, 1, 4, 1, 1, 3, 4, 4, 3, 4, 1, 3, 1, 2, 4, 1, 1, 1, 3, 1, 1, 1, 1, 2, 2, 9, 15, 1, 5, 1, 2, 22, 1, 1, 1, 1, 2, 1, 1, 5, 1, 1, 1, 3, 7, 5, 1, 1, 1, 1, 3, 1, 1, 2, 2, 1, 2, 1, 3, 2, 4, 1, 1, 2, 8, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 2, 1, 1, 2, 12, 2, 1, 8, 6, 2, 1, 1, 2, 3, 4,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1, 1, 2, 3, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "fonud_values_list = list(found.values())\n",
    "print(fonud_values_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "fonud_values_list.sort(reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[169, 38, 38, 22, 21, 21, 19, 16]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fonud_values_list[:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'传统': 169,\n",
       " '级融': 21,\n",
       " '新兴': 38,\n",
       " '智能': 16,\n",
       " '主流': 38,\n",
       " '推动': 19,\n",
       " '电视': 21,\n",
       " '广电': 22}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top8_found = {}\n",
    "for k,v in found.items():\n",
    "#     print(k,v)\n",
    "    if v in fonud_values_list[:8]:\n",
    "#         print(k,v)\n",
    "        top8_found[k]=v\n",
    "top8_found"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🙌🙌🙌🎈 👍 恭喜 👍 🎉🙌🙌🙌\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🏁🏁🏁回报答题分数（仅供参考）🏁🏁🏁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'总分': 140, 'details': {'freq_table_phrase': 20, 'list_split': 20, 'the_10_phrase': 20, 'position_all': 20, 'content_all': 20, 'found': 20, 'top8_found': 20}}\n"
     ]
    }
   ],
   "source": [
    "#终001 （勿改动，执行即可）回报答题分数\n",
    "import PandasCourse as PC\n",
    "\n",
    "score_details = PC.score_answers(locals(), _which_)\n",
    "print (score_details[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
